package my.soft.aiteacher.teacher.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jakarta.annotation.Resource;
import my.soft.aiteacher.exception.BusinessException;
import my.soft.aiteacher.teacher.mapStruct.ConceptMapStruct;
import my.soft.aiteacher.teacher.mapStruct.SmartAnalysisMapStruct;
import my.soft.aiteacher.teacher.mapper.ConceptExampleMapper;
import my.soft.aiteacher.teacher.mapper.ConceptMapper;
import my.soft.aiteacher.teacher.mapper.KnowledgeMapper;
import my.soft.aiteacher.teacher.mapper.SmartAnalysisMapper;
import my.soft.aiteacher.teacher.pojo.*;
import my.soft.aiteacher.teacher.pojo.KnowledgeType.Knowledge;
import my.soft.aiteacher.teacher.pojo.conceptExampleType.ApplicationScenarios;
import my.soft.aiteacher.teacher.pojo.conceptExampleType.ConceptExample;
import my.soft.aiteacher.teacher.pojo.conceptExampleType.Input;
import my.soft.aiteacher.teacher.pojo.conceptExampleType.Output;
import my.soft.aiteacher.teacher.service.IControlService;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class IControlServiceImpl implements IControlService {
    @Resource
    private SmartAnalysisMapper smartAnalysisMapper;
    @Resource
    private SmartAnalysisMapStruct smartAnalysisMapStruct;
    @Resource
    private ConceptMapper conceptMapper;
    @Resource
    private ConceptMapStruct conceptMapStruct;
    @Resource
    private ConceptExampleMapper conceptExampleMapper;
    @Resource
    private KnowledgeMapper knowledgeMapper;

    @Override
    public VideoInformationType detail(Long fileId) {
        if (Objects.isNull(fileId)) {
            throw new BusinessException("fileId为空");
        }
        SmartAnalysisDo smartAnalysisDo = smartAnalysisMapper.selectOne(new QueryWrapper<SmartAnalysisDo>().eq("file_id", fileId));

        List<ConceptDo> conceptDos = conceptMapper.selectList(new QueryWrapper<ConceptDo>().eq("file_id", fileId));
        List<ConceptExampleDo> conceptExampleDoList = conceptExampleMapper.selectList(new QueryWrapper<ConceptExampleDo>().eq("file_id", fileId));
       List< KnowledgeDo> knowledgeDos = knowledgeMapper.selectList(new QueryWrapper<KnowledgeDo>().eq("file_id", fileId));
        List<Concept> conceptList = new ArrayList<>();
        // 构建Concept对象
        conceptDos.forEach(conceptDo -> {
            Concept concept = Concept.builder()
                    .BasicConcepts(conceptDo.getBasicConcepts())
                    .validationTags(conceptDo.getValidationTags())
                    .build();
            conceptList.add(concept);
        });
        // 构建ConceptExample对象
        List<ConceptExample> conceptExamples = new ArrayList<>();
        for (ConceptExampleDo conceptExampleDo : conceptExampleDoList) {
            Input input = Input.builder()
                    .validationTags(conceptExampleDo.getValidationTags())
                    .basicConcepts(conceptExampleDo.getBasicConcepts())
                    .build();
            ApplicationScenarios applicationScenarios = ApplicationScenarios.builder()
                    .example(conceptExampleDo.getExample())
                    .contrast(conceptExampleDo.getContrast())
                    .build();
            Output output = Output.builder()
                    .applicationScenarios(applicationScenarios)
                    .principle(conceptExampleDo.getPrinciple())
                    .build();
            ConceptExample conceptExample = ConceptExample.builder()
                    .output(output)
                    .input(input)
                    .build();
            conceptExamples.add(conceptExample);
        }
        List<Knowledge> knowledgeList = new ArrayList<>();
        Knowledge knowledge = null;
        // 构建Knowledge对象
        if (Objects.nonNull(knowledgeDos)) {
            for (KnowledgeDo knowledgeDo: knowledgeDos) {
                List<my.soft.aiteacher.teacher.pojo.KnowledgeType.ApplicationScenarios> Knowledge_applicationScenarios = buildApplicationScenariosList(knowledgeDo.getApplicationScenarios());
                knowledge = Knowledge.builder()
                        .Outline(knowledgeDo.getOutline())
                        .MatchedTags(Arrays.stream(knowledgeDo.getMatchedTags().split(",")).toList())
                        .TechnicalSummary(knowledgeDo.getTechnicalSummary())
                        .ApplicationScenarios(Knowledge_applicationScenarios)
                        .ExtendedKeywords(Arrays.stream(knowledgeDo.getExtendedKeywords().split(",")).toList())
                        .build();
                knowledgeList.add(knowledge);
            }
        }

        // 构建VideoInformationType对象
        VideoInformationType videoInformationType = new VideoInformationType();
        if (Objects.nonNull(smartAnalysisDo)) {
            videoInformationType.setUnderstand(smartAnalysisDo.getContent());
        }
        videoInformationType.setConcept(conceptList);
        videoInformationType.setConceptExample(conceptExamples);
        videoInformationType.setKnowledge(knowledgeList);
        return videoInformationType;

    }

    private List<my.soft.aiteacher.teacher.pojo.KnowledgeType.ApplicationScenarios> buildApplicationScenariosList(String scenariosData) {
        List<my.soft.aiteacher.teacher.pojo.KnowledgeType.ApplicationScenarios> scenariosList = new ArrayList<>();
        if (scenariosData != null && !scenariosData.isEmpty()) {
            String[] scenarioItems = scenariosData.split(";");
            for (String item : scenarioItems) {
                String[] pairs = item.split(",");
                Map<String, String> params = new HashMap<>();
                for (String pair : pairs) {
                    String[] keyValue = pair.split("=");
                    if (keyValue.length == 2) {
                        params.put(keyValue[0].trim(), keyValue[1].trim());
                    }
                }
                my.soft.aiteacher.teacher.pojo.KnowledgeType.ApplicationScenarios applicationScenarios = my.soft.aiteacher.teacher.pojo.KnowledgeType.ApplicationScenarios.builder()
                        .Description(params.get("Description"))
                        .CodeExample(params.get("CodeExample"))
                        .build();
                scenariosList.add(applicationScenarios);
            }
        }
        return scenariosList;
    }
}
